

$(document).ready(function() {

	$('#submitBtn').on('click',function(){
		if($('#picture').val() == "" && ($('#picture').attr("src") == "" || $('#picture').attr("src")==undefined)){
			layer.msg("请上传封面",{time:1000});
			return false;
		}
		
		
		formValidate($(".form-horizontal"));
		
	});
	
	
	initFileUpload();
	var url = basePath+"/com/addCommodity";
	
	if($("#id").val() != "" || $("#id").val() == null){
		url = basePath+"/com/editCommodity";
		
	}
	
	
var formValidate = function(form){
		$(form).validate({
			rules:{
				commodity_name:{
					required:true,
				},
				specification:{
					required:true,
				},
				style:{
					required:true,
				},
				patten:{
					required:true,
				},
				origin:{
					required:true,
				},
				describes:{
					required:true,
				},
				start_date_time:{
					required:true,
				},
				end_date_time:{
					required:true,
				},
				start_price:{
					required:true,
					isNumber:true
				},
				add_price:{
					required:true,
					isNumber:true
				},
				deal_price:{
					required:true,
					isNumber:true
				},
				collection_num:{
					required:true,
					isNumber:true
				},
				browse_num:{
					required:true,
					isNumber:true
				},
			},
			onkeyup:false,
			focusCleanup:true,
			success:"valid",
			submitHandler:function(form){
				$(form).ajaxSubmit({
			        type:"post",
			        url:url,
			        async:false,
			        //beforeSubmit: showRequest,
			        success: function(data){
			        	 layer.msg(data.msg,{time:1000});
			        	 setTimeout(function(){
			        		 parent.location.reload();
			        		 var index = parent.layer.getFrameIndex(window.name);
				        	 parent.layer.close(index);
			        	 },1000)
			        	 
			        	
			        }
			      });
			}
		});
	}
});

//编辑时，初始化上传图片的插件，显示移除、修改按钮，隐藏选择图片按钮
var initFileUpload = function(){
	//获取图片的地址
 	$(".fileupload").each(function(){
	   var imgSrc=$(this).find("img")[0].src;
	   var bas = basePath+'/';
	 //如果存在图片，则进行移除、修改的显示，选择图片的隐藏
		if(undefined != imgSrc&&'' != imgSrc&& bas != imgSrc){
			//预览图片的显示
			$($(this).find(".fileupload-preview")[0]).show();
			//移除、修改的显示，选择图片的隐藏
			$(this).addClass('fileupload-exists').removeClass('fileupload-new');
		}
	});
	
}
